module Either
  def self.left(value) Left.new(value) end
  def self.right(value) Right.new(value) end
  
  class Base
    def initialize(value) @value = value end
    def left; end
    def right; end
  end
  
  class Left < Base
    def left; yield @value end
  end
  
  class Right < Base
    def right; yield @value end
  end
end

module Kernel
  def left(value)  Either::Left.new(value) end
  def right(value) Either::Right.new(value) end
  def either(f, g, value)
    value.left { |x| f[x] } \
    or value.right { |x| g[x] }
  end
end
